<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="user" @keyup.enter.native="userLogin">

        <el-form-item class="input-prepend restyle" prop="mobile" :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]">
          <div >
            <el-input type="text" placeholder="手机号" v-model="user.mobile"/>
            <i class="iconfont icon-phone" />
          </div>
        </el-form-item>

        <el-form-item class="input-prepend" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
          <div>
            <el-input type="password" placeholder="密码" v-model="user.password"/>
            <i class="iconfont icon-password"/>
          </div>
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-in-button" value="登录" @click="userLogin">
        </div>
      </el-form>
      <!-- 更多登录方式 -->
      <div class="more-sign">
        <h6>社交帐号登录</h6>
        <ul>
          <li><a id="weixin" class="weixin" href="http://localhost:8150/api/ucenter/wx/login"><i class="iconfont icon-weixin"/></a></li>
          <li><a id="qq" class="qq" target="_blank" href="#"><i class="iconfont icon-qq"/></a></li>
        </ul>
      </div>
    </div>

  </div>
</template>

<script>
  import '~/assets/css/sign.css'
  import '~/assets/css/iconfont.css'

  import loginApi from '@/api/login'
  import cookie from 'js-cookie'

  export default {
    layout: 'sign',

    data () {
      return {
        //登录的用户对象
        user:{
          //手机号
          mobile: '',
          password: ''
        },

        //获取到的用户信息
        userInfo: {}
      }
    },

    methods: {

      checkPhone (rule, value, callback) {
        //正则表达式校验手机号码
        if (!(/^1[34578]\d{9}$/.test(value))) {
          return callback(new Error('手机号码格式不正确'))
        }
        return callback()
      },

      //用户登录
      userLogin(){
        this.$refs.userForm.validate(async valid => {
          if (valid){
            const {data} = await loginApi.userLogin(this.user);
            if(data.code !== 20000) return this.$message.error(data.message);
            console.log(data);
            //登录成功后跳转到首页面并显示用户信息
            //登录后将token存放再cookie中
            //参数分别对应 cookie的名称 值 作用范围
            cookie.set('jn_cookie', data.data.data, {domain: 'localhost'});
            //发送请求获取用户信息
            const {data: res} = await loginApi.getUserInfo();
            if(data.code !== 20000) return this.$message.error(data.message);
            //将获取到的用户信息存放到token中
            this.userInfo = res.data.data;
            cookie.set('jn_user', this.userInfo, {domain: 'localhost'});
            this.$message.success('登录成功');
            //跳转到首页
            await this.$router.push("/");
          }
        })
      }
    }
  }
</script>

<style scoped>
  .el-form-item__error{
    z-index: 9999999;
  }
</style>
